package com.wl.common.security.config;

import com.wl.common.security.bean.IgnoreUrlsProperties;
import com.wl.common.security.interceptor.HeaderInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: wanglin
 * @date: 2024-03-07 周四
 * @Version: 1.0
 * @Description: 拦截器配置
 */
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private HeaderInterceptor headerInterceptor;
//    @Autowired
//    private AuthorityInterceptor authorityInterceptor;


    @Autowired
    private IgnoreUrlsProperties ignoreUrlList;

    /**
     * 不需要拦截地址
     */
    public static final String[] excludeUrls = {"/**/login", "/**/logout", "/refresh"};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(headerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(ignoreUrlList.getUrls())
                .order(-10);
//        registry.addInterceptor(authorityInterceptor);
    }

    /**
     * 跨域访问 配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //仅仅允许来自 domain2.com 的跨域访问，并且限定访问路径为／api 、方法是 POST 或者 GET 。
        registry.addMapping("*")
                .allowedOrigins("*")
                .allowedMethods("*");
    }

    /**
     * 配置静态访问资源
     *
     * @param registry
     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        for (String url : ignoreUrlList.getUrls()) {
//            registry.addResourceHandler(url);
//        }
//    }
}
